package com.strive.common.model.base;

import com.strive.common.enums.CodeEnum;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Objects;

/**
 * @description:
 * @author: bingcun.chen
 * @Date: 2022/2/24 13:26
 * @Version 1.0
 */
@Data
@NoArgsConstructor
public class Result<T> implements Serializable {
    private T data;
    private Integer status;
    private String message;
    private String requestId;

    public Result(T data, Integer code, String message, String requestId) {
        this.data = data;
        this.status = code;
        this.message = message;
        this.requestId = requestId;
    }

    public Result(T data, Integer code, String message) {
        this.data = data;
        this.status = code;
        this.message = message;
    }

    public static <T> Result<T> succeed() {
        return of(null, CodeEnum.SUCCESS.getCode(), null ,getRequestId());
    }

    public static <T> Result<T> succeed(String msg) {
        return of(null, CodeEnum.SUCCESS.getCode(), msg ,getRequestId());
    }

    public static <T> Result<T> succeed(T model, String msg) {
        return of(model, CodeEnum.SUCCESS.getCode(), msg,getRequestId());
    }

    public static <T> Result<T> succeed(T model) {
        return of(model, CodeEnum.SUCCESS.getCode(), "调用成功",getRequestId());
    }

    public static <T> Result<T> of(T data, Integer code, String msg, String requestId) {
        return new Result<>(data, code, msg,requestId);
    }

    public static <T> Result<T> of(T data, Integer code, String msg) {
        return new Result<>(data, code, msg);
    }

    public static <T> Result<T> failed(String msg) {
        return of(null, CodeEnum.ERROR.getCode(), msg,getRequestId());
    }

    public static <T> Result<T> failed(T model, String msg) {
        return of(model, CodeEnum.ERROR.getCode(), msg,getRequestId());
    }

    private static String getRequestId(){
        String requestId = "";
        if(Objects.nonNull(RequestModel.getRequestModel())){
            requestId = RequestModel.getRequestModel().getRequestId();
        }
        return requestId;
    }
}
